{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 判断两个字符串是否同构【中等】"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "判断字符串s是否由词典wordDict中的词构成，词典中的词可以重复使用，也可以有些词不用。\n",
    "\n",
    "例1：s = \"helloworld\"，wordDict = [\"hello\",\"word\",\"world\"] ，答案是True\n",
    "\n",
    "例2：s = \"helloworldhelloChina\"，wordDict = [\"hello\",\"China\",\"world\"] ，答案是True\n",
    "\n",
    "例3：s = \"helloworldhelloChina\"，wordDict = [\"hello\",\"word\",\"world\"] ，答案是False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#==============================答题区==============================#\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "#==============================答题区==============================#"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "#==============================提示区==============================#\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "#  提示：哈希表\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "#==============================提示区==============================#"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "True\n",
      "True\n",
      "False\n"
     ]
    }
   ],
   "source": [
    "#==============================答案区==============================#\n",
    "# 解题思路：回溯法\n",
    "# 尝试从wordDict中寻找和s的开头部分匹配的词，如果找到，将s砍去匹配部分得到新的s。\n",
    "# 继续在wordDict中寻找和新的s的开头部分匹配的词，直到s的长度变为0，返回True\n",
    "# 如果在当中任何一个步骤中失败了，返回上一步，寻找别的词进行匹配。\n",
    "# 如果尝试完所有可能后s的长度都没有变成0，返回False\n",
    "\n",
    "def f(s,wordDict):\n",
    "    d = [(0,wordDict[0])]\n",
    "    while True:\n",
    "        i,w = d[-1]\n",
    "        if s.startswith(w):\n",
    "            s = s[len(w):]\n",
    "            if len(s)==0:\n",
    "                return(True)\n",
    "            else:\n",
    "                d.append((0,wordDict[0]))\n",
    "        else:\n",
    "            if all([x[0]==len(wordDict)-1 for x in d]):\n",
    "                return(False)\n",
    "            if i==len(wordDict)-1:\n",
    "                d.pop()\n",
    "                s = d[-1][-1]+s\n",
    "            i,w = d[-1]\n",
    "            i,w = i+1,wordDict[i+1]\n",
    "            d[-1] = (i,w)        \n",
    "            \n",
    "\n",
    "print(f(\"helloworld\",[\"he\",\"hello\",\"world\",\"word\"]))\n",
    "print(f(\"helloworldhelloChina\",[\"hello\",\"world\",\"China\"]))\n",
    "print(f(\"helloworldhelloChina\",[\"hello\",\"world\",\"word\"]))     \n",
    "#==============================答案区==============================#"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "![](../Python与算法之美.jpg)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
